/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: timer.h
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#ifndef __MLSE_EVENT_TIMER_H__
#define __MLSE_EVENT_TIMER_H__

#include <tools/platform.h>
#include <string.h>

enum __enumTimerType
{
	eTimerTP_NONE = 0,
	eTimerTP_LOOP = 1,
	eTimerTP_ONCE = 2
};

typedef struct __mlse_timer
{
	int id;
	char tp;
	unsigned long interval;
	unsigned long last_run_time;
	bool is_complete;
	__mlse_timer()
	{
		memset(this, 0, sizeof(__mlse_timer));
		last_run_time = GetTimestamp();
	}

	void run()
	{
		last_run_time = GetTimestamp();
		if(tp == eTimerTP_ONCE)
			is_complete = true;
	}

	bool can_run()
	{
		return tp == eTimerTP_NONE ? false : (GetTimestamp() - last_run_time > interval);
	}

}mlse_timer, *mlse_timer_ptr;

class ITimer
{
public:
	virtual mlse_timer_ptr GetPtr(){ return &__timer; };
	virtual void OnTimer(unsigned long t){ __timer.run(); };
	static void CheckAllTimers();
	static void AddTimer(ITimer *);
	static void DelTimer(ITimer *);
	static void DelTimer(int id);
protected:
	static std::map<int, ITimer *> m_Timers;
	mlse_timer __timer;
};

#endif // __MLSE_SCRIPT_TIMER_H__
